package dev.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import org.junit.*;
import static org.junit.Assert.*;

public class SegmentBufferPrototype {

    private static final String outputBufferReadable = 
            "\0\0\0\0\0\0\0\0\0\0" 
            + "\0\0\0\0\0\0\0\0\0\0"
            + "\0\0\0\0\0\0\0\0\0\0"
            + "\0\0\0\0\0\0\0\0\0\0";
    
    private static final String inputBufferReadable = "This is the original\nCurrentLeftOver";
    private static final String previousLeftOverReadable = "PreviousLeftOver";
    private static final String currentLeftOverReadable = "CurrentLeftOver";

    private static final byte[] outputBuffer = outputBufferReadable.getBytes();
    private static final byte[] inputBuffer = inputBufferReadable.getBytes();
    private static final byte[] previousLeftOver = previousLeftOverReadable.getBytes();
    private static final byte[] currentLeftOver = currentLeftOverReadable.getBytes();

    @Test
    public void testAppendLeftOver() {
        
        int offset = determineSegmentContentOffset();
        
        String outputBuffer = "000" + previousLeftOverReadable + "This is the original\n";
        
        try {
            write(outputBuffer.getBytes(), offset);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private static int determineSegmentContentOffset() {
        int lengthBeforeAppendPreviousLeftOver = inputBuffer.length - currentLeftOver.length;
        int lengthAfterAppend = lengthBeforeAppendPreviousLeftOver + previousLeftOver.length;
        int startPosition = outputBuffer.length - lengthAfterAppend;
        return startPosition;
    }
    
    private void write(byte[] content, int offset) throws FileNotFoundException, IOException {
        File outputFile = new File("C:/test/testSegmentContent");
        FileOutputStream writer = new FileOutputStream(outputFile);        
        writer.write(content, offset, content.length - offset);
        writer.flush();
        writer.close();
    }

}
